{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.16666666666666666"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import math          ###distributionscategorical\n",
    "import functools\n",
    "import operator\n",
    "\n",
    "def cat(c, pi):\n",
    "    elems = [math.pow(pi_k, c_k) for (c_k, pi_k) in zip(c,pi)] #pi_k^c_k\n",
    "    return functools.reduce(operator.mul, elems)                      #リストの掛け算\n",
    "\n",
    "## サイコロを表現 ##\n",
    "pi = [1/6, 1/6, 1/6, 1/6, 1/6, 1/6]    #出目の確率が全て1/6\n",
    "c = [0,1,0,0,0,0]                                    #2の目が出るという事象の1-of-K表現\n",
    "cat(c, pi)                #確率を求めてみる -> 0.166666... = 1/6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "π= [0.15669654 0.08234161 0.17942086 0.23735849 0.3441825 ] 合計: 1.0\n",
      "π= [0.01782424 0.01752779 0.12175161 0.46410968 0.37878669] 合計: 1.0\n",
      "π= [0.01649573 0.2365099  0.22241829 0.30912348 0.2154526 ] 合計: 0.9999999999999999\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import dirichlet          ###distributionsdir\n",
    "\n",
    "tau = [1,2,3,4,5]\n",
    "\n",
    "for i in range(3):\n",
    "    pi = dirichlet(tau).rvs()[0]           #ディリクレ分布からπを生成\n",
    "    print(\"π=\", pi, \"合計:\", sum(pi))  #πの合計は1になる"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
